<template>
  <div class="playlist-container">
    <!-- 歌单详情 -->
    <div class="top-wrap">
      <!-- 封面 -->
      <div class="img-wrap">
        <img :src="playlist.coverImgUrl" alt="" />
      </div>
      <div class="info-wrap">
        <!-- 名字 -->
        <p class="title">{{ playlist.name }}</p>
        <!-- 头像 -->
        <div class="author-wrap">
          <!-- 神经病VUE,不加v-if="playlist.creator !== undefined" 报错 -->
          <img class="avatar" :src="playlist.creator.avatarUrl" v-if="playlist.creator !== undefined" alt="" />
          <!-- 名字 -->
          <span class="name" v-if="playlist.creator !== undefined">{{ playlist.creator.nickname }}</span>
          <!-- 时间 -->
          <span class="time">{{ playlist.createTime | dateFormat }}</span>
        </div>
        <!-- 播放全部-->
        <div class="play-wrap">
          <span class="iconfont icon-circle-play"></span>
          <span class="text">播放全部</span>
        </div>
        <!-- 标签-->
        <div class="tag-wrap">
          <span class="title">标签:</span>
          <ul>
            <li v-for="(item, index) in playlist.tags" :key="index">{{ item }}</li>
          </ul>
        </div>
        <!-- 简介-->
        <div class="desc-wrap">
          <span class="title">简介:</span>
          <span class="desc">{{ playlist.description }}</span>
        </div>
      </div>
    </div>

    <el-tabs v-model="activeIndex">

      <el-tab-pane label="歌曲列表" name="1">
        <table class="el-table playlit-table">
          <thead>
            <th></th>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr class="el-table__row" v-for="(item, index) in playlist.tracks" :key="index" @dblclick="playMusic(item.id,item.name,item.al.picUrl)">
              <td>{{ index + 1 }}</td>
              <td>
                <div class="img-wrap">
                  <img :src="item.al.picUrl+'?param=130y130'" alt="" />
                  <span class="iconfont icon-play" @click="playMusic(item.id,item.name,item.al.picUrl)"></span>
                </div>
              </td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <!-- 名称 -->
                    <span>{{ item.name }}</span>
                    <!-- mv图标 -->
                    <span v-if="item.mv !== 0" class="iconfont icon-mv" @click="toMv(item.mv)"></span>
                  </div>
                </div>
              </td>
              <td>{{ item.ar[0].name }}</td>
              <td>{{ item.al.name }}</td>
              <td>{{ item.dt | playTimeFormat }}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>

      <el-tab-pane :label="`评论(${total})`" name="2">

        <!-- 精彩评论 -->
        <div class="comment-wrap" v-if="hotComments !== undefined && hotComments.length !== 0">
          <p class="title">
            精彩评论<span class="number">({{ hotComments.length }})</span>
          </p>
          <div class="comments-wrap">
            <!-- 评论 -->
            <div class="item" v-for="(item, index) in hotComments" :key="index">
              <div class="icon-wrap">
                <!-- 头像 -->
                <img :src="item.user.avatarUrl+'?param=50y50'" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}</span>
                  <span class="comment">：{{ item.content }}</span>
                </div>
                <!-- 回复 -->
                <div class="re-content" v-if="item.beReplied.length !== 0">
                  <span class="name">{{ item.beReplied[0].user.nickname }}</span>
                  <span class="comment">：{{ item.beReplied[0].content }}</span>
                </div>
                <div class="date">{{ item.time | dateFormat }}</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 最新评论 -->
        <div class="comment-wrap">
          <p class="title">
            最新评论<span class="number">({{ total }})</span>
          </p>
          <div class="comments-wrap">
            <!-- 评论 -->
            <div class="item" v-for="(item, index) in comments" :key="index">
              <div class="icon-wrap">
                <!-- 头像 -->
                <img :src="item.user.avatarUrl+'?param=50y50'" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{ item.user.nickname }}</span>
                  <span class="comment">：{{ item.content }}</span>
                </div>
                <!-- 回复 -->
                <div class="re-content" v-if="item.beReplied.length !== 0">
                  <span class="name">{{ item.beReplied[0].user.nickname }}</span>
                  <span class="comment">：{{ item.beReplied[0].content }}</span>
                </div>
                <div class="date">{{ item.time | dateFormat }}</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 分页器 -->
        <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next" :total="total" :current-page="pageNum" :page-size="pageSize">
        </el-pagination>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>

import axios from 'axios'

export default {
  name: 'playlist',
  data () {
    return {
      id: '',
      activeIndex: '1',
      // 总条数
      total: 0,
      // 页码
      pageNum: 1,
      pageSize: 10,
      // 歌单内的音乐
      playlist: {},
      // 歌单热评
      hotComments: [],
      // 歌单评论
      comments: []
    }
  },
  created () {
    this.id = this.$route.query.id
    // 获取歌单详情
    this._getPlaylistDetail()
    // 获取歌单评论
    this._getPlaylistComments()
  },
  methods: {
    // 获取歌单详情
    async _getPlaylistDetail () {
      const { data: resp0 } = await this.getPlaylistDetail(this.id)
      this.playlist = resp0.playlist
      // console.log(resp0.playlist)
    },
    getPlaylistDetail (id) {
      return axios.post(`/playlist/detail/${id}`)
    },

    // 获取歌单评论
    async _getPlaylistComments () {
      const { data: resp1 } = await this.getPlaylistComments(this.id, (this.pageNum - 1) * this.pageSize)
      this.comments = resp1.comments
      this.hotComments = resp1.hotComments
      this.total = resp1.total
      // console.log(resp0.playlist)
    },
    getPlaylistComments (id, offset) {
      return axios.post(`/comment/playlist/${id}/${offset}`)
    },

    // 播放音乐
    async playMusic (id, name, picUrl) {
      // 获取歌曲播放地址
      const { data: resp } = await this.getMusicUrl(id)
      if (!resp.data[0].url) {
        return this.$message.error('该资源为VIP专享，暂不支持播放 ！')
      }
      // 设置给父组件的播放地址
      this.$parent.musicUrl = resp.data[0].url
      this.$parent.namees = name
      this.$parent.imgUrl = picUrl
    },
    getMusicUrl (id) {
      return axios.post(`/song/url/${id}`)
    },

    // 进入mv详情页面
    toMv (id) {
      this.$router.push(`/mv?id=${id}`)
    },

    // 分页
    handleCurrentChange (val) {
      this.pageNum = val
      this._getPlaylistComments()
    }
  }
}
</script>

<style >
</style>
